feat: 实现 sub2api 审计请求转换服务 - #1
Merged
Merged
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
关键安全与鲁棒性问题需要修复(审计输入标签注入风险、readyz 未鉴权泄露错误细节、以及并发统计一致性风险)。
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
本 PR 将仓库从原先的 petsitter fork 代码库重构为一个独立的 sub2api Prompt Audit 适配服务:提供管理网页用于配置上游审核模型,并对外暴露 OpenAI-compatible 的 /v1/models 与 /v1/chat/completions,把 sub2api 的审计请求转换、转发并将上游输出标准化为 sub2api 期望的 Safety/Categories 格式。
Changes:
- 新增
sub2api_auditer核心实现(配置存储、协议封装、上游调用、输出归一化、Starlette Web/API)。 - 新增管理网页静态资源与 Docker/Docker Compose/CI 测试矩阵。
- 清理并移除 petsitter 相关源码、示例与旧测试,替换为面向本服务的新测试集。
File summaries
| File | Description |
|---|---|
| tests/test_trickset_log.py | 移除 petsitter trickset 日志端点相关测试。 |
| tests/test_trick.py | 移除 petsitter trick 基础能力测试。 |
| tests/test_trick_config.py | 移除 petsitter per-trick 配置与 API 测试。 |
| tests/test_secrets_protector.py | 移除 petsitter SecretsProtectorTrick 测试。 |
| tests/test_rules_file.py | 移除 petsitter RulesFileTrick 测试。 |
| tests/test_registry_e2e.py | 移除 petsitter registry 端到端脚本式测试。 |
| tests/test_recommender_list.py | 移除 petsitter recommender list trick 测试。 |
| tests/test_playground_e2e.py | 移除 petsitter Playwright 浏览器 E2E 测试。 |
| tests/test_observability.py | 移除 petsitter observability 与代理日志路由测试。 |
| tests/test_protocol.py | 新增协议/归一化逻辑单测(URL 构造、审计文本提取、输出解析/拒绝)。 |
| tests/test_config.py | 新增配置更新行为单测(空值保留 API key、显式清空 API key)。 |
| tests/test_app.py | 新增应用级单测(上游转发、sub2api 响应 envelope、鉴权与错误映射)。 |
| src/sub2api_auditer/web.py | 新增 Starlette Web/API(管理页面、健康检查、配置读写、审计接口、鉴权)。 |
| src/sub2api_auditer/static/index.html | 新增内置中文管理页面 HTML。 |
| src/sub2api_auditer/static/app.js | 新增管理页面前端逻辑(配置读写、状态轮询、测试调用、token 存储)。 |
| src/sub2api_auditer/static/app.css | 新增管理页面样式。 |
| src/sub2api_auditer/static/init.py | 新增静态资源包标记。 |
| src/sub2api_auditer/service.py | 新增上游审核调用服务(httpx 连接池、限长读取、统计、错误包装)。 |
| src/sub2api_auditer/protocol.py | 新增协议层(请求文本抽取、上游 payload 构造、OpenAI 响应/错误封装)。 |
| src/sub2api_auditer/normalize.py | 新增上游输出归一化解析(JSON/flagged/confidence/文本等多格式兼容)。 |
| src/sub2api_auditer/config.py | 新增配置模型与原子落盘(版本冲突、脱敏字段、环境变量默认值)。 |
| src/sub2api_auditer/app.py | 新增 CLI 启动入口(uvicorn run 参数与导出 app)。 |
| src/sub2api_auditer/init.py | 新增包元信息(版本号导出)。 |
| src/petsitter/tricksets/opencode.json | 移除 petsitter 预置 trickset 配置。 |
| src/petsitter/tricksets/gemma4.json | 移除 petsitter 预置 trickset 配置。 |
| src/petsitter/tricks/xml_tool.py | 移除 petsitter trick 实现。 |
| src/petsitter/tricks/tool_call.py | 移除 petsitter trick 实现。 |
| src/petsitter/tricks/swapharness.py | 移除 petsitter trick 实现。 |
| src/petsitter/tricks/self_improver.py | 移除 petsitter trick 实现。 |
| src/petsitter/tricks/secrets_protector.py | 移除 petsitter trick 实现。 |
| src/petsitter/tricks/rules_file.py | 移除 petsitter trick 实现。 |
| src/petsitter/tricks/no_emdash.py | 移除 petsitter trick 实现。 |
| src/petsitter/tricks/multiround.py | 移除 petsitter trick 实现。 |
| src/petsitter/tricks/multiconsult.py | 移除 petsitter trick 实现。 |
| src/petsitter/tricks/mcp_tools.py | 移除 petsitter trick 实现。 |
| src/petsitter/tricks/kennel.py | 移除 petsitter trick 实现。 |
| src/petsitter/tricks/json_mode.py | 移除 petsitter trick 实现。 |
| src/petsitter/tricks/exportit.py | 移除 petsitter trick 实现。 |
| src/petsitter/tricks/code_validator.py | 移除 petsitter trick 实现。 |
| src/petsitter/tricks/init.py | 移除 petsitter tricks 包入口。 |
| src/petsitter/observability.py | 移除 petsitter 可观测性实现。 |
| src/petsitter/loader.py | 移除 petsitter 动态加载器实现。 |
| src/petsitter/gui/vendor/github-dark.min.css | 移除 petsitter GUI vendor 资源。 |
| src/petsitter/gui/swagger.html | 移除 petsitter Swagger 页面。 |
| src/petsitter/context.py | 移除 petsitter context 工具。 |
| src/petsitter/agents/opencode.py | 移除 petsitter agent harness。 |
| src/petsitter/agents/codex.py | 移除 petsitter agent harness。 |
| src/petsitter/agents/claude_code.py | 移除 petsitter agent harness。 |
| src/petsitter/agents/init.py | 移除 petsitter agents 基础定义。 |
| src/petsitter/agent_manager.py | 移除 petsitter agent manager。 |
| src/petsitter/.agents/skills/self-improver/SKILL.md | 移除 petsitter 内置技能文档。 |
| src/petsitter/.agents/skills/petsitter-create-trick/SKILL.md | 移除 petsitter 内置技能文档。 |
| src/petsitter/.agents/skills/petsitter-create-trick/references/trick-api.md | 移除 petsitter 内置技能参考文档。 |
| src/petsitter/.agents/skills/petsitter-create-trick/references/hook-examples.md | 移除 petsitter 内置技能参考文档。 |
| src/petsitter/.agents/skills/petsitter-create-trick/assets/trick-template.py | 移除 petsitter 内置技能模板。 |
| src/petsitter/.agents/skills/petsitter-create-agent/SKILL.md | 移除 petsitter 内置技能文档。 |
| src/petsitter/init.py | 移除 petsitter 包导出。 |
| pyproject.toml | 更新项目元数据/依赖/脚本入口以适配 sub2api-auditer。 |
| petsitter | 移除旧 petsitter CLI 入口脚本。 |
| examples/modelset.json | 移除 petsitter 示例配置。 |
| examples/evals/scorers.py | 移除 petsitter eval 示例。 |
| examples/evals/scenarios.py | 移除 petsitter eval 示例。 |
| examples/evals/runner.py | 移除 petsitter eval 示例。 |
| examples/evals/report.py | 移除 petsitter eval 示例。 |
| Dockerfile | 新增容器镜像构建与健康检查。 |
| docker-compose.yml | 新增一键部署编排与数据卷。 |
| .gitignore | 精简并新增运行时配置/数据目录忽略规则。 |
| .github/workflows/test.yml | 新增 pytest 多版本矩阵与 Docker build 校验。 |
| .env.example | 新增环境变量示例与安全提示。 |
| .dockerignore | 新增 Docker 构建上下文忽略规则。 |
Review details
- Files reviewed: 83/98 changed files
- Comments generated: 4
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+96
to
+106
| def build_upstream_payload(*, model: str, prompt: str, text: str, max_tokens: int) -> dict[str, Any]: | ||
| return { | ||
| "model": model, | ||
| "messages": [ | ||
| {"role": "system", "content": f"{prompt.strip()}\n\n{OUTPUT_CONTRACT}"}, | ||
| {"role": "user", "content": f"<audit_input>\n{text}\n</audit_input>"}, | ||
| ], | ||
| "temperature": 0, | ||
| "max_tokens": max_tokens, | ||
| "stream": False, | ||
| } |
Comment on lines
+96
to
+100
| "status": "ready" if status == 200 else "not_ready", | ||
| "configured": config.ready, | ||
| "config_version": config.version, | ||
| "config_error": store.load_error, | ||
| }, status_code=status) |
Comment on lines
+182
to
+183
| except ProtocolError as exc: | ||
| return _error(str(exc), "invalid_audit_request", 413 if "过大" in str(exc) else 400) |
Comment on lines
+97
to
+99
| self.stats.total += 1 | ||
| self.stats.in_flight += 1 | ||
| self.stats.last_request_at = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
变更概览
将原 Petsitter fork 重构为面向 sub2api Prompt Audit 的独立轻量适配器:
/v1/models与/v1/chat/completions,兼容 sub2api 节点探测和正式审核请求flagged/confidence、Markdown JSON 代码块及 Qwen3Guard 文本输出Safety: ...\nCategories: ...后套入 OpenAI 响应 envelopeADMIN_TOKEN与AUDITER_TOKEN两层可选鉴权.env.example本地验证
PYTHONPATH=src pytest -q:13 passedpython -m compileall -q src tests:通过sub2api 推荐配置
http://sub2api-auditer:8080sub2api-auditerAUDITER_TOKEN一致